package cn.ming.wfchat.app.me.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.wildfirechat.remote.MD5Util;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020/9/7 3:15 PM
 * 设置解锁密码
 */
public class SetUnlockActivity extends WfcBaseActivity {

    @BindView(R.id.oldpasswordEditText)
    EditText oldpasswordEditText;
    @BindView(R.id.fl_old_password)
    FrameLayout flOldPassword;
    @BindView(R.id.passwordEditText)
    EditText passwordEditText;
    @BindView(R.id.fl_password)
    FrameLayout flPassword;
    @BindView(R.id.et_repeat)
    EditText etRepeat;
    @BindView(R.id.loginButton)
    Button loginButton;
    boolean modify;
    private SharedPreferences sp;

    @Override
    protected int contentLayout() {
        return R.layout.activity_set_unlock;
    }

    @Override
    protected void afterViews() {
        setTitle("解锁密码");
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        modify = getIntent().getBooleanExtra("modify", false);
        if (modify) {
            flOldPassword.setVisibility(View.VISIBLE);
        } else {
            flOldPassword.setVisibility(View.GONE);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick(R.id.loginButton)
    void login() {
        String passWord = passwordEditText.getText().toString().trim();
        if (passWord.length() != 6) {
            ChatManager.showToast("密码必须为6位纯数字");
            return;
        }
        if (!passWord.equals(etRepeat.getText().toString().trim())) {
            ChatManager.showToast("密码不一致");
            return;
        }
        loginButton.setEnabled(false);
        if (modify) {
            AppService.Instance().replaceLockPassword(MD5Util.getMD5String(oldpasswordEditText.getText().toString().trim()),
                    MD5Util.getMD5String(passWord), new AppService.SendCodeCallback(){
                @Override
                public void onUiSuccess() {
                    if (isFinishing()) {
                        return;
                    }
                    sp.edit().putString("lockPassword",MD5Util.getMD5String(passWord))
                            .apply();
                    ChatManager.showToast("操作成功");
                    finish();
                }

                @Override
                public void onUiFailure(String msg) {
                    if (isFinishing()) {
                        return;
                    }
                    ChatManager.showToast(msg);
                    loginButton.setEnabled(true);
                }
            });
        } else {
            AppService.Instance().addLockPassword(MD5Util.getMD5String(passWord), new AppService.SendCodeCallback() {
                @Override
                public void onUiSuccess() {
                    if (isFinishing()) {
                        return;
                    }
                    sp.edit().putString("lockPassword",MD5Util.getMD5String(passWord))
                            .apply();
                    ChatManager.showToast("操作成功");
                    finish();
                }

                @Override
                public void onUiFailure(String msg) {
                    if (isFinishing()) {
                        return;
                    }
                    ChatManager.showToast(msg);
                    loginButton.setEnabled(true);
                }
            });
        }
    }


}
